#### Recode values to prepare for estimation
#### Jonne Kamphorst
#### February 2021



#### Load libraries and data ####
library(dplyr)
library(tidyverse)
library(gdata) #for keep

all_data <- readRDS("data/final_data_long_all.rds")






#### Recode and rename variables ####
## Rename variables from agreement ##
all_data <- all_data %>% rename(force_immi = QPT1.1_1,
                                force_eu = QPT1.1_2,
                                force_climate = QPT1.1_3,
                                force_lgbt = QPT1.1_4,
                                force_wage = QPT1.1_5,
                                force_farms = QPT1.1_6)


## Rename QV variables ##
all_data <- all_data %>% rename(QV_immi = QV1,
                                QV_wage = QV2,
                                QV_eu = QV3,
                                QV_climate = QV4,
                                QV_farms = QV5,
                                QV_lgbt = QV6)





# Create absolute values from QV variables
all_data$QV_immi_abs <- abs(all_data$QV_immi)
all_data$QV_wage_abs <- abs(all_data$QV_wage)
all_data$QV_eu_abs <- abs(all_data$QV_eu)
all_data$QV_climate_abs <- abs(all_data$QV_climate)
all_data$QV_farms_abs <- abs(all_data$QV_farms)
all_data$QV_lgbt_abs <- abs(all_data$QV_lgbt)



## Check how many credits people used
all_data$credits <- (all_data$QV_immi_abs)^2 +
  (all_data$QV_wage_abs)^2 +
  (all_data$QV_eu_abs)^2 +
  (all_data$QV_climate_abs)^2 +
  (all_data$QV_farms_abs)^2 +
  (all_data$QV_lgbt_abs)^2




# remove people with inconsistencies between forced choice and QV
all_data$wrongly_understood_qv <- 0

all_data$wrongly_understood_qv[all_data$QV_wage > 0 & all_data$force_wage == "Oneens"] <- all_data$wrongly_understood_qv[all_data$QV_wage > 0 & all_data$force_wage == "Oneens"] + 1
all_data$wrongly_understood_qv[all_data$QV_wage < 0 & all_data$force_wage == "Eens"] <- all_data$wrongly_understood_qv[all_data$QV_wage < 0 & all_data$force_wage == "Eens"] + 1

all_data$wrongly_understood_qv[all_data$QV_eu > 0 & all_data$force_eu == "Oneens"] <- all_data$wrongly_understood_qv[all_data$QV_eu > 0 & all_data$force_eu == "Oneens"] + 1
all_data$wrongly_understood_qv[all_data$QV_eu < 0 & all_data$force_eu == "Eens"] <- all_data$wrongly_understood_qv[all_data$QV_eu < 0 & all_data$force_eu == "Eens"] + 1

all_data$wrongly_understood_qv[all_data$QV_climate > 0 & all_data$force_climate == "Oneens"] <- all_data$wrongly_understood_qv[all_data$QV_climate > 0 & all_data$force_climate == "Oneens"] + 1
all_data$wrongly_understood_qv[all_data$QV_climate < 0 & all_data$force_climate == "Eens"] <- all_data$wrongly_understood_qv[all_data$QV_climate < 0 & all_data$force_climate == "Eens"] + 1

all_data$wrongly_understood_qv[all_data$QV_immi > 0 & all_data$force_immi == "Oneens"] <- all_data$wrongly_understood_qv[all_data$QV_immi > 0 & all_data$force_immi == "Oneens"]  + 1
all_data$wrongly_understood_qv[all_data$QV_immi < 0 & all_data$force_immi == "Eens"] <- all_data$wrongly_understood_qv[all_data$QV_immi < 0 & all_data$force_immi == "Eens"] + 1

all_data$wrongly_understood_qv[all_data$QV_farms > 0 & all_data$force_farms == "Oneens"] <- all_data$wrongly_understood_qv[all_data$QV_farms > 0 & all_data$force_farms == "Oneens"] + 1
all_data$wrongly_understood_qv[all_data$QV_farms < 0 & all_data$force_farms == "Eens"] <- all_data$wrongly_understood_qv[all_data$QV_farms < 0 & all_data$force_farms == "Eens"] + 1

all_data$wrongly_understood_qv[all_data$QV_lgbt > 0 & all_data$force_lgbt == "Oneens"] <- all_data$wrongly_understood_qv[all_data$QV_lgbt > 0 & all_data$force_lgbt == "Oneens"] + 1
all_data$wrongly_understood_qv[all_data$QV_lgbt < 0 & all_data$force_lgbt == "Eens"] <- all_data$wrongly_understood_qv[all_data$QV_lgbt < 0 & all_data$force_lgbt == "Eens"]  + 1

table(all_data$wrongly_understood_qv)


#remove respondents based on paying attention.
all_data <- subset(all_data, wrongly_understood_qv < 3) #more than two inconsitencies
#all_data <- subset(all_data, `Duration (in seconds)` > 120) #less than two minutes




## Rename thermostat variables ##
all_data <- all_data %>% rename(therm_vvd = Q3.3,
                                therm_pvv = Q3.4,
                                therm_cda = Q3.5,
                                therm_d66 = Q3.6,
                                therm_gl = Q3.7,
                                therm_sp = Q3.8,
                                therm_pvda = Q3.9,
                                therm_fvd = Q3.10,
                                therm_cu = Q3.11,
                                therm_denk = Q3.12,
                                therm_sgp = Q3.13,
                                elec_today = Q2.1)

therms <- c("therm_vvd", "therm_pvv", "therm_cda", "therm_d66", "therm_gl",
            "therm_sp", "therm_pvda", "therm_fvd", "therm_cu", "therm_denk", "therm_sgp")

#change values in all columns to numeric
all_data <- all_data %>% mutate_at(therms, 
                                   ~as.numeric(recode(.,
                                                      "Zeer onsympathiek: 0"=0, 
                                                      "1"=1,
                                                      "2"=2,
                                                      "3"=3,
                                                      "4"=4,
                                                      "5"=5,
                                                      "6"=6,
                                                      "7"=7,
                                                      "8"=8,
                                                      "9"=9,
                                                      "Zeer sympathiek: 10"=10,
                                                      "Weet ik niet"=-99))) #change to -99 if you want to drop these people!

#change the 99 to NA
all_data <- all_data %>% dplyr::na_if(-99)






# change party names #
all_data$Partij <- as.character(all_data$Partij)
all_data$Partij[all_data$Partij == "CDA (Christen Democratisch Appel)"] <- "CDA"
all_data$Partij[all_data$Partij == "VVD (Volkspartij voor Vrijheid en Democratie)"] <- "VVD"
all_data$Partij[all_data$Partij == "SP (Socialistische Partij)"] <- "SP"
all_data$Partij[all_data$Partij == "PvdA (Partij van de Arbeid)"] <- "PvdA"
all_data$Partij[all_data$Partij == "PVV (Partij Voor de Vrijheid)"] <- "PVV"
all_data$Partij[all_data$Partij == "GL (Groen Links)"] <- "GL"

all_data$elec_today <- as.character(all_data$elec_today)
all_data$elec_today[all_data$elec_today == "GroenLinks"] <- "GL"







#### Add salience to the long data format ####
## Create a small salience dataframe that is also long
sal_frame <- all_data %>% select(rid, QV_immi_abs, QV_wage_abs, 
                                 QV_eu_abs, QV_lgbt_abs, QV_farms_abs, 
                                 QV_climate_abs) %>% #select salience columns
  rename(`immi` = QV_immi_abs, #rename for pivot longer
         `wage` = QV_wage_abs,
         `farms` = QV_farms_abs,
         `EU` = QV_eu_abs,
         `lgbt` = QV_lgbt_abs,
         `climate` = QV_climate_abs)  %>% 
  pivot_longer(!rid, names_to="issue", values_to="salience") %>% #pivot longer
  distinct(issue, rid, .keep_all = T) #drop duplicates. 

all_data <- left_join(all_data, sal_frame)
rm(sal_frame)


all_data$salience_dummies[all_data$salience >= 0 & all_data$salience <= 1] <- "Low" # 0 - 0
all_data$salience_dummies[all_data$salience >= 2 & all_data$salience <= 3] <- "Medium" # 1- 3
all_data$salience_dummies[all_data$salience >= 4 & all_data$salience <= 6] <- "High" # 4 - 6

all_data$salience_dummies <- factor(all_data$salience_dummies, levels=c("Low", "Medium", "High"))

table(all_data$salience_dummies)







#### Change the strategies to align with agree or disagree with a voter####
# Ambiguity first
all_data$strategy <- as.character(all_data$strategy)
all_data$strategy[all_data$strategy == "Schengen kan in gevaar komen of niet maar solidariteit is een fundamentele afspraak binnen Schengen"] <- "Ambiguity"
all_data$strategy[all_data$strategy == "We toetsen planologische regels voor de bouw van zeer grote stallen op volksgezondheidsaspecten. Waar nodig worden de regels aangescherpt"] <- "Ambiguity"
all_data$strategy[all_data$strategy == "Er is natuurlijk ook wel een klimaatprobleem maar mensen die dat ontkennen mogen dat"] <- "Ambiguity"
all_data$strategy[all_data$strategy == "Scholen zijn wettelijk verplicht zorg te dragen voor de sociale psychische en fysieke veiligheid van alle leerlingen"] <- "Ambiguity"
all_data$strategy[all_data$strategy == "In de huidige situatie moeten we ons volledig focussen on het beteugelen van een recessie en het behouden van zoveel mogelijk banen. Er is voeding voor een debat"] <- "Ambiguity"
all_data$strategy[all_data$strategy == "Ik ben trots op democratie en zal de resultaten van een referendum accepteren"] <- "Ambiguity"


##  Then whether a voter agrees or disagrees with a party
all_data$strategy_recoded <- all_data$strategy

#EU
all_data$strategy_recoded[all_data$strategy_recoded == "Wij zijn een voorstander van een referendum over de EU" & all_data$QV_eu > 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Wij zijn een voorstander van een referendum over de EU" & all_data$QV_eu == 0 & all_data$force_eu == "Eens"] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Er moet geen referendum komen" & all_data$QV_eu < 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Er moet geen referendum komen" & all_data$QV_eu == 0 & all_data$force_eu == "Oneens"] <- "Agree"

all_data$strategy_recoded[all_data$strategy_recoded == "Wij zijn een voorstander van een referendum over de EU" & all_data$QV_eu < 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Wij zijn een voorstander van een referendum over de EU" & all_data$QV_eu == 0 & all_data$force_eu == "Oneens"] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Er moet geen referendum komen" & all_data$QV_eu > 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Er moet geen referendum komen" & all_data$QV_eu == 0 & all_data$force_eu == "Eens"] <- "Disagree"


#immi
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten migranten toelaten" & all_data$QV_immi > 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten migranten toelaten" & all_data$QV_immi == 0 & all_data$force_immi == "Eens"] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten immigratie stoppen" & all_data$QV_immi < 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten immigratie stoppen" & all_data$QV_immi == 0 & all_data$force_immi == "Oneens"] <- "Agree" 

all_data$strategy_recoded[all_data$strategy_recoded == "We moeten migranten toelaten" & all_data$QV_immi < 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten migranten toelaten" & all_data$QV_immi == 0 & all_data$force_immi == "Oneens"] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten immigratie stoppen" & all_data$QV_immi > 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten immigratie stoppen" & all_data$QV_immi == 0 & all_data$force_immi == "Eens"] <- "Disagree" 


#wage
all_data$strategy_recoded[all_data$strategy_recoded == "Wij komen met een wetsvoorstel voor een hoger minimumloon" & all_data$QV_wage > 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Wij komen met een wetsvoorstel voor een hoger minimumloon" & all_data$QV_wage == 0 & all_data$force_wage == "Eens"] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Het minimumloon moet niet omhoog" & all_data$QV_wage < 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Het minimumloon moet niet omhoog" & all_data$QV_wage == 0 & all_data$force_wage == "Oneens"] <- "Agree"

all_data$strategy_recoded[all_data$strategy_recoded == "Wij komen met een wetsvoorstel voor een hoger minimumloon" & all_data$QV_wage < 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Wij komen met een wetsvoorstel voor een hoger minimumloon" & all_data$QV_wage == 0 & all_data$force_wage == "Oneens"] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Het minimumloon moet niet omhoog" & all_data$QV_wage > 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Het minimumloon moet niet omhoog" & all_data$QV_wage == 0 & all_data$force_wage == "Eens"] <- "Disagree"


#lgbtq
all_data$strategy_recoded[all_data$strategy_recoded == "Homoseksualiteit afkeuren mag" & all_data$QV_lgbt > 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Homoseksualiteit afkeuren mag" & all_data$QV_lgbt == 0 & all_data$force_lgbt == "Eens"] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Scholen mogen homoseksualiteit niet afkeuren" & all_data$QV_lgbt < 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Scholen mogen homoseksualiteit niet afkeuren" & all_data$QV_lgbt == 0 & all_data$force_lgbt == "Oneens"] <- "Agree"

all_data$strategy_recoded[all_data$strategy_recoded == "Homoseksualiteit afkeuren mag" & all_data$QV_lgbt < 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Homoseksualiteit afkeuren mag" & all_data$QV_lgbt == 0 & all_data$force_lgbt == "Oneens"] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Scholen mogen homoseksualiteit niet afkeuren" & all_data$QV_lgbt > 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Scholen mogen homoseksualiteit niet afkeuren" & all_data$QV_lgbt == 0 & all_data$force_lgbt == "Eens"] <- "Disagree"


#farms
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten stoppen met de megastallen" & all_data$QV_farms > 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten stoppen met de megastallen" & all_data$QV_farms == 0 & all_data$force_farms == "Eens"] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Megastallen mogen" & all_data$QV_farms < 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "Megastallen mogen" & all_data$QV_farms == 0 & all_data$force_farms == "Oneens"] <- "Agree"

all_data$strategy_recoded[all_data$strategy_recoded == "We moeten stoppen met de megastallen" & all_data$QV_farms < 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten stoppen met de megastallen" & all_data$QV_farms == 0 & all_data$force_farms == "Oneens"] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Megastallen mogen" & all_data$QV_farms > 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "Megastallen mogen" & all_data$QV_farms == 0 & all_data$force_farms == "Eens"] <- "Disagree"



#climate
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten snel in actie komen" & all_data$QV_climate > 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten snel in actie komen" & all_data$QV_climate == 0 & all_data$force_climate == "Eens"] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "We hoeven niet snel in actie te komen" & all_data$QV_climate < 0] <- "Agree"
all_data$strategy_recoded[all_data$strategy_recoded == "We hoeven niet snel in actie te komen" & all_data$QV_climate == 0 & all_data$force_climate == "Oneens"] <- "Agree"

all_data$strategy_recoded[all_data$strategy_recoded == "We moeten snel in actie komen" & all_data$QV_climate < 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "We moeten snel in actie komen" & all_data$QV_climate == 0 & all_data$force_climate == "Oneens"] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "We hoeven niet snel in actie te komen" & all_data$QV_climate > 0] <- "Disagree"
all_data$strategy_recoded[all_data$strategy_recoded == "We hoeven niet snel in actie te komen" & all_data$QV_climate == 0 & all_data$force_climate == "Eens"] <- "Disagree"






#### Add partisanship ####
## Create a variable for co-partisan or not
all_data$partisan <- ifelse(all_data$Partij == "CDA" & all_data$elec_today == "CDA" | 
                              all_data$Partij == "CDA" & all_data$therm_cda > 6, 1, 0)

all_data$partisan <- ifelse(all_data$Partij == "GL" & all_data$elec_today == "GL" | 
                              all_data$Partij == "GL" & all_data$therm_gl > 6, 1, all_data$partisan)

all_data$partisan <- ifelse(all_data$Partij == "VVD" & all_data$elec_today == "VVD" | 
                              all_data$Partij == "VVD" & all_data$therm_vvd > 6, 1, all_data$partisan)

all_data$partisan <- ifelse(all_data$Partij == "PvdA" & all_data$elec_today == "PvdA" | 
                              all_data$Partij == "PvdA" & all_data$therm_pvda > 6, 1, all_data$partisan)

all_data$partisan <- ifelse(all_data$Partij == "PVV" & all_data$elec_today == "PVV" | 
                              all_data$Partij == "PVV" & all_data$therm_pvv > 6, 1, all_data$partisan)

all_data$partisan <- ifelse(all_data$Partij == "SP" & all_data$elec_today == "SP" | 
                              all_data$Partij == "SP" & all_data$therm_sp > 6, 1, all_data$partisan)



## Create an alternative variable for co-partisan or not
all_data$partisan_alt <- ifelse(all_data$Partij == "CDA" & all_data$therm_cda > 6, 1, 0)
all_data$partisan_alt <- ifelse(all_data$Partij == "GL" & all_data$therm_gl > 6, 1, all_data$partisan_alt)
all_data$partisan_alt <- ifelse(all_data$Partij == "VVD" & all_data$therm_vvd > 6, 1, all_data$partisan_alt)
all_data$partisan_alt <- ifelse(all_data$Partij == "PvdA" & all_data$therm_pvda > 6, 1, all_data$partisan_alt)
all_data$partisan_alt <- ifelse(all_data$Partij == "PVV" & all_data$therm_pvv > 6, 1, all_data$partisan_alt)
all_data$partisan_alt <- ifelse(all_data$Partij == "SP" & all_data$therm_sp > 6, 1, all_data$partisan_alt)




## Create an alternative variable for co-partisan or not
all_data$partisan_altelect <- ifelse(all_data$Partij == "CDA" & all_data$elec_today == "CDA", 1, 0)
table(all_data$partisan_altelect)
all_data$partisan_altelect <- ifelse(all_data$Partij == "GL" & all_data$elec_today == "GL", 1, all_data$partisan_altelect)
table(all_data$partisan_altelect)
all_data$partisan_altelect <- ifelse(all_data$Partij == "VVD" & all_data$elec_today == "VVD", 1, all_data$partisan_altelect)
table(all_data$partisan_altelect)
all_data$partisan_altelect <- ifelse(all_data$Partij == "PvdA" & all_data$elec_today == "PvdA", 1, all_data$partisan_altelect)
table(all_data$partisan_altelect)
all_data$partisan_altelect <- ifelse(all_data$Partij == "PVV" & all_data$elec_today == "PVV", 1, all_data$partisan_altelect)
table(all_data$partisan_altelect)
all_data$partisan_altelect <- ifelse(all_data$Partij == "SP" & all_data$elec_today == "SP", 1, all_data$partisan_altelect)
table(all_data$partisan_altelect)



## Create a continues variables for copartisan or not #NAs are in there because of don't know category
all_data$therm_all <- ifelse(all_data$Partij == "CDA", all_data$therm_cda, 
                             ifelse(all_data$Partij == "VVD", all_data$therm_vvd,
                                    ifelse(all_data$Partij == "PvdA", all_data$therm_pvda,
                                           ifelse(all_data$Partij == "PVV", all_data$therm_pvv,
                                                  ifelse(all_data$Partij == "SP", all_data$therm_sp,
                                                         ifelse(all_data$Partij == "GL", all_data$therm_gl, NA))))))







## Add the strategy of the opponent as other variable
# Group by RID. mutife with if conditions. 
all_data <- all_data %>% group_by(rid, task, issue) %>% 
  mutate(strategy_opponent = ifelse(profile == 1, strategy_recoded[profile == 2], 
                                    ifelse(profile == 2, strategy_recoded[profile == 1], NA))) 









#write_rds(all_data, "data/final_data_long_all_recoded.rds")


